// campus_whole_manager.h
#pragma once

#include <memory>
#include "../sql/campus_sql_connector.h"
#include "../login/campus_login_manager.h"
#include "campus_whole_server.h"
#include"../person_info/campus_person_info_define.h"
#include"../person_info/campus_person_info_manager.h"
#include"../club_info/campus_club_info_manager.h"
#include"../live_info/campus_live_info_manager.h"

class CampusWholeManager : 
    public CampusSqlConnectorObserver, 
    public CampusLoginServerObserver,
    public CampusPersonInfoObserver,
    public CampusClubInfoObserver,
    public CampusLiveInfoObserver
{
public:
    CampusWholeManager();
    virtual ~CampusWholeManager() = default;

    // 启动所有资源（服务器、SQL连接、登录管理器）
    void start();
    // 等待线程（阻塞，直到资源退出）
    void wait();

protected:
    // -------------------------- CampusLoginServerObserver 接口实现（转发给LoginManager）--------------------------
    virtual void handleLoginSalt(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleLogin(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleRegister(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleLogout(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleHdlClose(websocketpp::connection_hdl hdl) override;

    // -------------------------- CampusSqlConnectorObserver 接口实现（转发给LoginManager）--------------------------
    virtual void onSelectSucess(std::vector<std::vector<std::pair<std::string, std::string>>>& result, CampusSqlInfo* userData) override;
    virtual void onSelectFail(CampusSqlInfo* userData) override;
    virtual void onUpdateSucess(CampusSqlInfo* userData) override; // 登录业务暂不用Update，空实现
    virtual void onUpdateFail(CampusSqlInfo* userData) override;   // 空实现
    virtual void onInsertSucess(CampusSqlInfo* userData) override;
    virtual void onInsertFail(CampusSqlInfo* userData) override;

    // -------------------------- CampusPersonInfoObserver 接口实现（转发给PersonInfoManager）--------------------------
    virtual void handlePersonInfo(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handlePersonInfoModify(websocketpp::connection_hdl hdl, const json JMsg) override;

    // -------------------------- CampusClubInfoObserver 接口实现（转发给ClubInfoManager）--------------------------
    virtual void handleClubCreate(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleClubSearch(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleClubJoin(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleClubGroup(websocketpp::connection_hdl hdl, const json JMsg) override;
    virtual void handleClubInfo(websocketpp::connection_hdl hdl, const json JMsg)override;
    virtual void handleGroupChat(websocketpp::connection_hdl hdl, const json JMsg)override;

    virtual void handleLiveJoin(websocketpp::connection_hdl hdl, const json JMsg)override;
    virtual void handleLiveLeave(websocketpp::connection_hdl hdl)override;
    virtual void handleLiveChat(websocketpp::connection_hdl hdl, const json JMsg)override;

private:
    void initLoginManager();
    void initPersonInfoManager();
    void initClubInfoManager();
    void initLiveManager();

private:
    CampusWholeServer* whole_server_;                  // 服务器实例（单例）
    CampusSqlConnector* sql_connector_ = nullptr;// SQL连接实例
    std::shared_ptr<CampusLoginManager> login_manager_;// 登录业务管理器（核心业务委托）

    std::shared_ptr<CampusPersonInfoManager>person_info_manager_;
    std::shared_ptr<CampusClubInfoManager>club_info_manager_;

    std::shared_ptr<CampusLiveInfoManager>live_info_manager_;
};